package com.oauth.auth.shared;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gwt.user.server.rpc.RPC;
import com.oauth.auth.client.beans.MemberClientBean;
import com.oauth.components.client.rpc.exception.SessionExpiredException;
import com.oauth.service.members.beans.Member;

public class AuthUtil {

	private static final String CHARSET_UTF8 = "UTF-8";
	private static final String CONTENT_TYPE_TEXT_PLAIN_UTF8 = "text/plain; charset=utf-8";

	public static void writeResponse(HttpServletRequest request, HttpServletResponse response) {
		String payload = null;
		try {
			payload = RPC.encodeResponseForFailure(null, new SessionExpiredException(
					"Session Expired"));
			byte[] reply = payload.getBytes(CHARSET_UTF8);
			// Send the reply.
			response.setContentLength(reply.length);
			response.setContentType(CONTENT_TYPE_TEXT_PLAIN_UTF8);
			response.setStatus(HttpServletResponse.SC_OK);
			response.getOutputStream().write(reply);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
	}

	public static MemberClientBean getMember(Member pMember) {
		MemberClientBean memberClient = new MemberClientBean();
		memberClient.setEmail(pMember.getEmail());
		memberClient.setName(pMember.getNickName());
		memberClient.setId(pMember.getMemberId());
		return memberClient;
	}
}
